﻿using CloudDiamonds.Models.DBModels;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.Xml;

namespace WholesalerService.Common
{
    public class RapnetPriceProvider
    {
        XmlDocument xmldoc = new XmlDocument();
        decimal[] areaRound = new decimal[] { 0.01M, 0.04M, 0.08M, 0.15M, 0.18M, 0.23M, 0.30M, 0.40M, 0.50M, 0.70M, 0.90M, 1.00M, 1.50M, 2.00M, 3.00M, 4.00M, 5.00M, 10.00M };
        decimal[] areaSpecial = new decimal[] { 0.18M, 0.23M, 0.30M, 0.40M, 0.50M, 0.70M, 0.90M, 1.00M, 1.50M, 2.00M, 3.00M, 4.00M, 5.00M, 10.00M };

        public RapnetPriceProvider(string priceXml)
        {
            xmldoc.Load(AppDomain.CurrentDomain.BaseDirectory + priceXml);
        }

        //public RapnetPriceProvider(string priceXml)
        //{
        //    var path = AppDomain.CurrentDomain.BaseDirectory + priceXml;
        //    xmldoc.Load(path);
        //    if (HttpRuntime.Cache["priceXml"] == null)  //过一段时间检查
        //    {
        //        UpdateByPricexml(path);  //更新path，这个path服务器上得给相应写入权限
        //    }
        //}      

      
        public decimal GetCaratLevel(int shape, decimal carat)
        {
            decimal result = 0;
            decimal[] currentArea = shape == 1 ? areaRound : areaSpecial;

            foreach (decimal item in currentArea)
            {
                if (carat < item)
                {
                    break;
                }
                result = item;
            }
            return result;
        }

        public int GetPrice(InventoryInfo model)
        {
            if (!model.Weight.HasValue || model.Weight <= 0)
            {
                return 0;
            }


            string shape = model.Shape == 1 ? "round" : "other";
            //FL净度按IF计算
            string color = model.Color.ToUpper();
            string clarity = model.Clarity.ToUpper();
            if (!new string[] { "D", "E", "F", "G", "H", "I", "J", "K", "L", "M" }.Contains(color))
            {
                color = "M";
            }

            string key = shape + "_" + this.GetCaratLevel(model.Shape, (decimal)model.Weight).ToString("f2") + "_" + color + "_" + (clarity == "FL" ? "IF" : clarity);
            XmlNode node = xmldoc.SelectSingleNode("/items/item[@key='" + key + "']");
            if (node != null)
            {
                return Convert.ToInt32(node.Attributes["value"].InnerText);
            }

            return 0;
        }

        public int GetPrice(OrderInfo model)
        {
            if (!model.Weight.HasValue || model.Weight <= 0)
            {
                return 0;
            }


            string shape = model.Shape == 1 ? "round" : "other";
            //FL净度按IF计算
            string color = model.Color.ToUpper();
            string clarity = model.Clarity.ToUpper();
            if (!new string[] { "D", "E", "F", "G", "H", "I", "J", "K", "L", "M" }.Contains(color))
            {
                color = "M";
            }

            string key = shape + "_" + this.GetCaratLevel(model.Shape, (decimal)model.Weight).ToString("f2") + "_" + color + "_" + (clarity == "FL" ? "IF" : clarity);
            XmlNode node = xmldoc.SelectSingleNode("/items/item[@key='" + key + "']");
            if (node != null)
            {
                return Convert.ToInt32(node.Attributes["value"].InnerText);
            }

            return 0;
        }
        public int GetPrice(Order model)
        {
            Inventory _inventory = JsonConvert.DeserializeObject<Inventory>(model.InventoryAttribute);
            if (!_inventory.Weight.HasValue || _inventory.Weight <= 0)
            {
                return 0;
            }


            string shape = _inventory.Shape == 1 ? "round" : "other";
            //FL净度按IF计算
            string color = _inventory.Color.ToUpper();
            string clarity = _inventory.Clarity.ToUpper();
            if (!new string[] { "D", "E", "F", "G", "H", "I", "J", "K", "L", "M" }.Contains(color))
            {
                color = "M";
            }

            string key = shape + "_" + this.GetCaratLevel(_inventory.Shape, (decimal)_inventory.Weight).ToString("f2") + "_" + color + "_" + (clarity == "FL" ? "IF" : clarity);
            XmlNode node = xmldoc.SelectSingleNode("/items/item[@key='" + key + "']");
            if (node != null)
            {
                return Convert.ToInt32(node.Attributes["value"].InnerText);
            }

            return 0;
        }

        public int GetPrice(System.Data.DataRow row)
        {
            decimal weight = Convert.ToDecimal(row["Weight"]);

            if (weight <= 0)
            {
                return 0;
            }
            string shape = row["Shape"].ToString() == "1" ? "round" : "other";
            //FL净度按IF计算
            string color = row["Color"].ToString().ToUpper();
            string clarity = row["Clarity"].ToString().ToUpper();
            if (!new string[] { "D", "E", "F", "G", "H", "I", "J", "K", "L", "M" }.Contains(color))
            {
                color = "M";
            }

            string key = shape + "_" + this.GetCaratLevel((shape == "round" ? 1 : 2), weight).ToString("f2") + "_" + color + "_" + (clarity == "FL" ? "IF" : clarity);
            XmlNode node = xmldoc.SelectSingleNode("/items/item[@key='" + key + "']");
            if (node != null)
            {
                return Convert.ToInt32(node.Attributes["value"].InnerText);
            }
            return 0;
        }      

    }
}